﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;

namespace GolfLeague.Controllers
{
    public class HandicapController : Controller
    {
        HandicapInterface handicapInterface = new HandicapInterface();
        PlayerInterface playerInterface = new PlayerInterface();
        ScheduleInterface scheduleInterface = new ScheduleInterface();
        TeamInterface teamInterface = new TeamInterface();

        //
        // GET: /Handicap/

        public ActionResult Index()
        {
            List<PlayersHandicap> players = new List<PlayersHandicap>();
            IEnumerable<Team> teamList = teamInterface.GetAllActiveTeams();

            foreach (Team team in teamList)
                foreach (Player teamPlayer in teamInterface.GetTeamPlayers(team.TeamID))
                {
                    PlayerHandicap previous = handicapInterface.GetPreviousHandicap(teamPlayer.PlayerID);
                    players.Add(new PlayersHandicap
                    {
                        CurrentHandicap = handicapInterface.GetCurrentHandicap(teamPlayer.PlayerID),
                        TeamID = team.TeamID,
                        PreviousHandicap = (previous == null ? 0 : previous.Handicap)
                    });
                }

            return View(new HandicapEditModel
            {
                Players = players,
                Schedules = scheduleInterface.GetAllSchedules()
            });
        }

    }
}
